package com.mf.ergate.web.config;

import com.mf.ergate.web.interceptor.PjaxInterceptor;
import com.mf.ergate.web.common.utils.StringToDateConverter;
import com.mf.ergate.web.interceptor.AccountExposingHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Date;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	/**
	 * 配置静态访问资源
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
	}

	/**
	 * 拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new AccountExposingHandlerInterceptor()).addPathPatterns("/", "/web/**");
		registry.addInterceptor(new PjaxInterceptor()).addPathPatterns("/", "/web/**");
	}

	/**
	 * 增加字符串转日期的功能
	 */
	@Bean
	public Converter<String, Date> addStringToDateConvert() {
		return new StringToDateConverter();
	}
}
